package annuaire.beans;

import java.io.Serializable;
import java.util.Collection;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Version;

/**
 * Bean représentant une promotion.
 * 
 * @author Yannick Lorenzati & Stephanie Malakian
 * 
 */

@Entity(name = "Promotion")
public class Promotion implements Serializable, Comparable<Promotion> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1314152507379707183L;

	@Id()
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;

	@Basic(optional = false)
	@Column(name = "promoYear", length = 200, nullable = false, unique = false)
	private String promoYear;

	@Basic(optional = false)
	@Column(name = "title", length = 200, nullable = false, unique = false)
	private String title;

	@Basic()
	@Column(name = "description", length = 300, nullable = true, unique = false)
	private String description;

	// Remove en cascade (si on supprime une promo => a supprimera
	// automatiquement les etudiants qui en font partie)
	/**
	 * Relation Un-Plusieurs (une promotion peut avoir 0, 1 ou plusieurs
	 * etudiants)
	 */
	@OneToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST,
			CascadeType.REMOVE }, fetch = FetchType.LAZY, mappedBy = "promo")
	private Collection<Student> listStudents;

	@Version
	private long version = 0;

	public Promotion() {
	}

	public Promotion(String promoYear, String title) {
		super();
		this.promoYear = promoYear;
		this.title = title;
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("\nPromotion : " + promoYear + " " + title + " "
				+ description);
		return buffer.toString();
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the promoYear
	 */
	public String getPromoYear() {
		return promoYear;
	}

	/**
	 * @param promoYear
	 *            the promoYear to set
	 */
	public void setPromoYear(String promoYear) {
		this.promoYear = promoYear;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @param listStudents
	 *            the listStudents to set
	 */
	public void setListStudents(Collection<Student> listStudents) {
		this.listStudents = listStudents;
	}

	/**
	 * @return the listStudents
	 */
	public Collection<Student> getListStudents() {
		return listStudents;
	}

	public void setVersion(long version) {
		this.version = version;
	}

	public long getVersion() {
		return version;
	}
	
	@Override
	public int compareTo(Promotion p2) {
		if( this.promoYear.compareTo(p2.getPromoYear()) < 0 ){
            return -1;
        }
        else if( this.promoYear.compareTo(p2.getPromoYear()) == 0 ){
            return 0;
        }
        else{
            return 1;
        }
	}


}
